// @flow
import React, { Component } from 'react'
import styles from './ScatterHistoryLine.css'
import Highcharts from 'highcharts-release'
// import HighStock from 'highcharts-release/highstock.js'
import R from 'ramda'

type Props = {
  title?: string,
  yTitle?: string,
  field: string,
  chartData: Array<Object>
}
class ScatterHistoryLine extends Component {
  props: Props
  draw (chartData: any) {
    // const that = this
    const container = this.refs.container
    Highcharts.setOptions({ global: { useUTC: false } })
    let innerChart = Highcharts.chart(container, {
      chart: {
        backgroundColor: 'transparent',
        zoomType: 'x'
      },
      title: {
        text: this.props.title,
        x: -20
      },
      rangeSelector : {
        enabled: false,
        buttons : [{
          type : 'all',
          count : 1,
          text : 'All'
        }],
        selected : 1,
        inputEnabled : false
      },
      credits:{
        enabled: false
      },
      legend: {
        enabled: true,
        verticalAlign: 'top'
      },
      tooltip: {
        shared: true,
        useHTML: true,
        formatter: function () {
          let t = '<table><tr><td style="color: ' + this.series.color + '">' + this.series.name + ': </td>' +
            '<td style="text-align: right"><b>' + this.y + '</b></td></tr>'
          t += '</table>'
          return t
        },
        valueDecimals: 2
      },
      xAxis: {
        // categories: this.props.xData,
        plotBands: [{
          color: 'rgba(68, 170, 213, .2)'
        }]
      },
      yAxis: {
        title: {
          text: this.props.yTitle
        },
        plotLines: [{
          value: 0,
          width: 1,
          color: '#808080'
        }]
      },
      plotOptions: {
        line: {
          lineWidth: 1,
          dataGrouping: {
            enabled: false
          }
        },
        series: {
          animation: true
        }
      },
      series: chartData
    })
    setTimeout(() => {
      if (innerChart) {
        innerChart.reflow()
      }
    }, 20)
  }

  componentDidMount () {
    const chartData = this.renderData(this.props)
    this.draw(chartData)
  }

  componentWillReceiveProps (nextProps: Props) {
    if (!R.equals(nextProps, this.props)) {
      const chartData = this.renderData(nextProps)
      this.draw(chartData)
    }
  }
  renderData (props: Props) {
    const { field, chartData } = props
    const cloneData = R.clone(chartData)

    const selectDatas = cloneData.filter((item) => {
      return item.name === field
    })[0]
    const filterDatas = cloneData.filter((item) => {
      return item.name !== field
    })

    return filterDatas.map((item, index) => {
      const innerData = []
      selectDatas.data.map((datas, i) => {
        innerData.push([datas[1], item.data[i][1]])
      })
      item.data = innerData
      return item
    })
  }
  render () {
    return (
      <div ref='container' className={styles['container']} />
    )
  }
}

export default ScatterHistoryLine
